package com.example.weddingapp.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.lifecycle.AndroidViewModel;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.weddingapp.R;
import com.example.weddingapp.activity.ActivityDetailActivity;
import com.example.weddingapp.activity.AddInfoActivity;
import com.example.weddingapp.adapter.ActivityAdapter;
import com.example.weddingapp.base.BaseFragment;
import com.example.weddingapp.databinding.FragmentActivityBinding;
import com.example.weddingapp.databinding.FragmentChildActivityBinding;
import com.example.weddingapp.utils.RefreshHelper;

import java.util.Date;
import java.util.List;

import cn.leancloud.AVObject;
import cn.leancloud.AVQuery;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import me.jingbin.library.ByRecyclerView;

/**
 * @date 2020/5/7 9:11
 * @desc 活动2个子页面
 */
public class ActivityChildFragment extends BaseFragment<AndroidViewModel, FragmentChildActivityBinding> {
    private ActivityAdapter adapter;
    private int page = 1;
    private int pageSize = 30;
    private ActivityChildFragment instance;
    private int position;//0:未完成  1：已完成

    public static ActivityChildFragment getInstance(int position) {
        ActivityChildFragment childFragment = new ActivityChildFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("position", position);
        childFragment.setArguments(bundle);
        return childFragment;
    }


    private ActivityChildFragment() {
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        position = getArguments().getInt("position");
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        showContentView();

    }

    @Override
    public int setContent() {
        return R.layout.fragment_child_activity;
    }


    @Override
    protected void loadData() {
        super.loadData();
        bindingView.fabAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), AddInfoActivity.class));
            }
        });
        adapter = new ActivityAdapter();
        bindingView.recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));
        // 加了分割线，滚动条才会置顶
        RefreshHelper.initStaggeredGrid(bindingView.recyclerView, 2, 10);

        bindingView.recyclerView.setAdapter(adapter);
        bindingView.recyclerView.setOnRefreshListener(new ByRecyclerView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                getData();
            }
        });
        bindingView.recyclerView.setOnLoadMoreListener(new ByRecyclerView.OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        }, 300);
        bindingView.recyclerView.setOnItemClickListener(new ByRecyclerView.OnItemClickListener() {
            @Override
            public void onClick(View v, int position) {
                Intent intent = new Intent(getContext(), ActivityDetailActivity.class);
                AVObject object = adapter.getData().get(position);
                intent.putExtra("json", object.toJSONString());
                startActivity(intent);
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        page = 1;
        getData();
    }

    /**
     * 加载失败后点击后的操作
     */
    @Override
    protected void onRefresh() {
        page = 1;
        getData();
    }

    private void getData() {
        if (page == 1) {
            adapter.clear();
            adapter.notifyDataSetChanged();
        }
        AVQuery<AVObject> query = new AVQuery<>("Activity");
        if (position == 0){
            query.whereGreaterThanOrEqualTo("timeDate",new Date());
        }else {
            query.whereLessThan("timeDate",new Date());
        }

        query.include("image");
        query.include("user");
        // 按 createdAt 降序排列
        query.orderByDescending("createdAt");
        // 最多获取 pageSize 条结果
        query.limit(pageSize);
        // 跳过前 pageSize 条结果
        query.skip((page - 1) * pageSize);
        query.findInBackground().subscribe(new Observer<List<AVObject>>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(List<AVObject> list) {
                showContentView();
                bindingView.recyclerView.setRefreshing(false);
                if (list != null && list.size() > 0) {
                    if (page == 1) {
                        showContentView();
                        adapter.setNewData(list);
                    } else {
                        adapter.addData(list);
                    }
                    bindingView.recyclerView.loadMoreComplete();
                    if (list.size() < pageSize) {
                        bindingView.recyclerView.loadMoreEnd();
                    }
                } else {
                    if (page == 1) {
                        showToast("暂无数据");
                    } else {
                        bindingView.recyclerView.loadMoreEnd();
                    }
                }
            }

            @Override
            public void onError(Throwable e) {
                showContentView();
                showToast("暂无数据");
                bindingView.recyclerView.setRefreshing(false);
                bindingView.recyclerView.loadMoreEnd();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}